package com.xinqi.modules.prepare.common.utils;

import com.xinqi.common.base.dto.QuestionBaseDTO;
import com.xinqi.common.base.dto.utils.QuestionStats;
import com.xinqi.common.base.utils.json.JSON;
import com.xinqi.common.base.utils.number.BigDecimals;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * @author linkai
 * @date 2023/3/28
 */
public interface QuestionUtils {

    static QuestionStats status(List<QuestionBaseDTO> questionList) {
        if (CollectionUtils.isEmpty(questionList)) {
            return QuestionStats.empty();
        }
        BigDecimal totalScore = BigDecimal.ZERO;
        int questionTotal = 0;
        Set<String> questionTypeSet = new HashSet<>(10);
        for (QuestionBaseDTO questionBaseDTO : questionList) {
            totalScore = BigDecimals.add(Optional.ofNullable(questionBaseDTO.getScore()).orElse(BigDecimal.ZERO), totalScore);
            questionTotal++;
            questionTypeSet.add(questionBaseDTO.getType());
        }
        return QuestionStats.of(totalScore, questionTotal, questionTypeSet.size());
    }

    static QuestionStats status(String questionJson) {
        return status(json(questionJson));
    }

    static List<QuestionBaseDTO> json(String questionJson) {
        if (StringUtils.isBlank(questionJson)) {
            return Collections.emptyList();
        }
        return JSON.parseList(questionJson, QuestionBaseDTO.class);
    }
}
